<?php
/* 
* [***********************]
* @author: mazhenyu[houdunwangmzy@163.com]
* @Date:   2014-07-01 23:26:32
* @Last Modified time: 2014-07-01 23:31:37
*/
class Resume7Model extends Model{
	public $table = "people_resume7";


	/**
	 * [get_data 取得单条数据]
	 * @param  [type] $reid [关联第一阶段简历表id]
	 * @return [type]       [description]
	 */
	public function get_data($reid){
		return $this->where(array('resume1_reid'=>$reid))->find();
	}

	/**
	 * [update_face 修改或者上传头像]
	 * @param  [type] $reid [description]
	 * @return [type]       [description]
	 */
	public function update_face($reid){
		//实例化上传类
		$upload = new Upload();
		$upfiles = $upload->upload();

		//如果有错误
		if(!$upfiles){
			$this->error = $upload->error;
			return false;
		}

		$where = array('resume1_reid'=>$reid);
		//数据库获得缩略图地址
		$thumbPath = $this->where($where)->getField('face');

		$data = array(
			'resume1_reid'	=>	$reid,
			'face'			=>	$upfiles[0]['thumb']
			);
		//如果存在，则需要修改，不存在直接添加
		if($thumbPath){
			//删除本地文件
			$this->del_pic($reid);
			$this->where($where)->save($data);
		}else{
			$this->add($data);
			//增加完成步骤
			K('Resume1')->add_step($reid,7);
		}
		return true;        
	}


	/**
	 * [del_pic 删除头像]
	 * @param  [type] $thumbPath [缩略图地址]
	 * @return [type]            [description]
	 */
	public function del_pic($reid){
		$where = array('resume1_reid'=>$reid);
		//数据库获得缩略图地址
		$thumbPath = $this->where($where)->getField('face');
		//upload/12312312.jpg
		//组合缩略图完整路径
		$thumbFullPath = ROOT_PATH . $thumbPath;

		//如果缩略图存在删除，并且删除原图
		if(is_file($thumbFullPath)){
			//删除缩略图
			unlink($thumbFullPath);

			$info = pathinfo($thumbPath);
			//组合原图地址
			$sourcePath = substr($thumbPath, 0, strpos($thumbPath, '_')) . '.' . $info['extension'];
			$sourceFullPath = ROOT_PATH . $sourcePath;

			is_file($sourceFullPath) && unlink($sourceFullPath);

		}
	}


}
?>
